//
//  PersonnelListViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/14.
//

#import "PersonnelListViewController.h"
#import "PersonnelListTableViewCell.h"
#import "PersonnelModel.h"
#import "PersonnelDetailsViewController.h"
#import "PersonnelSeachViewController.h"
#import "foundQuestViewController.h"
#import "getPondTissueListModel.h"

@interface PersonnelListViewController ()<UITableViewDelegate>
@property (nonatomic ,strong) NSMutableArray <PersonnelModel *>* modelArray;
@property (nonatomic ,strong) NSArray <NSArray <PositionsModel *>*>* PositionsArray;

@property (nonatomic ,strong) UILabel * countLab;
@property (nonatomic ,strong) UIButton * SXBtn;
@property (nonatomic ,strong) UIView * sxview;
@property (nonatomic ,strong) UIScrollView * sxScrollView;


@end

@implementation PersonnelListViewController
- (NSMutableArray<PersonnelModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    // Do any additional setup after loading the view.
}
- (void)setupView{
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(120));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"人员管理" andFont:kCFont(18) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    UIImageView * rightimg = kimageView(@"seach");
    rightimg.addClickBlock(^{
        [weakSelf openSS];
    });
    [topview addSubview:rightimg];
    [rightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(29);
        make.right.offset(kwt(-15));
        make.centerY.equalTo(backview);
    }];
    UIView * ssview = [UIView new];
    ssview.backgroundColor = KcolorHex(@"#F1F5FA");
    [self.view addSubview:ssview];
    [ssview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(kwt(48));
        make.top.equalTo(topview.mas_bottom);
    }];
    
    UILabel * countLab = [DTKTool labelWithText:@"人员信息" andFont:kCFont(20) andTextColor:KcolorHex(@"#222222")];
    self.countLab = countLab;
    [ssview addSubview:countLab];
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
    }];
    
    UIView * lineview = [UIView new];
    lineview.backgroundColor =KcolorHex(@"#1983FA");
    [DTKTool YJ:kwt(1.5) andView:lineview];
    [ssview addSubview:lineview];
    [lineview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(60));
        make.width.offset(kwt(26));
        make.height.offset(kwt(3));
        make.bottom.offset(0);
    }];
    
    UIButton * SXBtn = [UIButton buttonWithType:0];
    self.SXBtn = SXBtn;
    SXBtn.addTarget(self,@selector(SXBtnClick));
    SXBtn.addNormalText(@"筛选").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [SXBtn setTitleColor:KcolorHex(@"#0967FF") forState:UIControlStateSelected];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen"] forState:UIControlStateNormal];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen2"] forState:UIControlStateSelected];
    [SXBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kwt(50), 0, 0)];
    [SXBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, kwt(-40), 0, 0)];
    [ssview addSubview:SXBtn];
    [SXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.offset(0);
        make.width.offset(kwt(80));
    }];
    UIView * ssline = [UIView new];
    ssline.backgroundColor = KcolorHex(@"#C4C4C4");
    [ssview addSubview:ssline];
    [ssline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.offset(0.5);
        make.right.equalTo(SXBtn.mas_left);
        make.height.offset(kwt(14));
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.top.equalTo(ssview.mas_bottom);
    }];
  
    [self loadNewData];
    self.sxview.hidden = YES;
    [self loadZWApi];
 
    [self addHeaderView:^{
        [weakSelf loadNewData];
    }];
    [self addFooterView:^{
        [weakSelf loadNewData];
    }];
}
- (void)openSS{
    [self.navigationController pushViewController:[PersonnelSeachViewController new] animated:YES];
}

- (void)SXBtnClick{
    self.sxview.hidden = !self.sxview.hidden;
    
}
///职位
- (void)loadZWApi{
    [SCNetManger PostappAccountGetPositionsWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:PositionsModel.class json:model.result];
            NSMutableArray * PositionsArray = [NSMutableArray array];
            NSMutableArray * oneArray = [NSMutableArray array];
            PositionsModel * qbm = [PositionsModel new];
            qbm.label = @"全部";
            qbm.value = @"";
            qbm.isSeled = YES;
            [oneArray addObject:qbm];
            [oneArray addObjectsFromArray:array];
            [PositionsArray addObject:oneArray];
            NSMutableArray * strarray = [NSMutableArray array];
            for (NSString * str in @[@"全部",@"已完成",@"待完成"]) {// 1 未完成 2 已完成
                PositionsModel * m = [PositionsModel new];
                m.isNameType = YES;
                m.label = str;
                if([str isEqualToString:@"全部"]){
                    m.isSeled = YES;
                }
                if([str isEqualToString:@"已完成"]){
                    m.value = @"2";
                } else if([str isEqualToString:@"待完成"]){
                    m.value = @"1";
                }
                [strarray addObject:m];
            }
            [PositionsArray addObject:strarray];
            self.PositionsArray = PositionsArray;
            [self addSXview];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (UIView *)sxview{
    if(!_sxview){
        
        _sxview = [UIView new];
        self.sxview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self.view addSubview:_sxview];
        [_sxview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.bottom.equalTo(self.tableview);
        }];
        self.sxview.hidden = YES;
        UIView * backview = [UIView new];
        backview.backgroundColor = UIColor.whiteColor;
        [_sxview addSubview:backview];
        [backview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.offset(0);
            make.height.offset(kwt(355));
        }];
        
        self.sxScrollView = [UIScrollView new];
        self.sxScrollView.frame = CGRectMake(0, 0, KSCREEN_WIDTH, kwt(285));
        [backview addSubview:self.sxScrollView];
        
        
        UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
        [qxbtn setTitle:@"重置" forState:UIControlStateNormal];
        [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
        qxbtn.titleLabel.font = kCFont(17);
        [DTKTool YJ:kwt(5) andView:qxbtn];
        [qxbtn addTarget:self action:@selector(leftBtnclick) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:qxbtn];
        [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(44));
            make.width.offset(kwt(160));
            make.left.offset(kwt(20));
            make.bottom.offset(kwt(-12));
        }];
        
        UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(160), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
        [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
        [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        qrbtn.titleLabel.font = kCFont(17);
        [DTKTool YJ:kwt(5) andView:qrbtn];
        [qrbtn addTarget:self action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:qrbtn];
        [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(44));
            make.width.offset(kwt(160));
            make.right.offset(kwt(-20));
            make.bottom.offset(kwt(-12));
        }];
        
    }
    return _sxview;
}
- (void)leftBtnclick{
    for (NSArray< PositionsModel * >* modelArray in self.PositionsArray) {
        for (PositionsModel * model in modelArray) {
            model.isSeled = model == modelArray.firstObject;
        }
    }
    self.SXBtn.selected = NO;
    [self addSXview];
}
- (void)rightbtnclick{
    self.sxview.hidden = YES;
    [self loadNewData];
}


- (void)addSXview{
    [self.sxScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat X = kwt(15);
    CGFloat Y = kwt(18);
    int i = 0;
    for (NSArray< PositionsModel * >* modelArray in self.PositionsArray) {
        X = kwt(15);
        UILabel * toplab = [DTKTool labelWithText: (i == 0)?@"岗位职责":@"任务完成情况" andFont:kCFont(17) andTextColor:KcolorHex(@"#242729")];
        [self.sxScrollView addSubview:toplab];
        toplab.frame = CGRectMake(X, Y, kwt(200), kwt(17));
        i++;
        Y += kwt(31);
        for (PositionsModel * model in modelArray) {
            gywbtn * btn = [gywbtn buttonWithType:0];
            btn.addNormalText(model.label).addNormalColor(KcolorHex(model.isSeled?@"#0669FE":@"#242729")).addFont(model.isSeled?kCFont(16):kFont(16)).addbackgroundColor(KcolorHex(model.isSeled?@"#EDF4FF":@"#F2F4F6"));
            [DTKTool YJBK:kwt(5) andView:btn andWidth:0.5 andColor:KcolorHex(model.isSeled?@"#0669FE":@"#DFDFDF")];
            btn.infoDict = @{@"model":model};
            if(model.value.length > 0 || modelArray != self.PositionsArray.firstObject){
                btn.addTarget(self,@selector(zwBtnClick:));
            } else {
                btn.addTarget(self,@selector(qbBtnclick));
            }
            [self.sxScrollView addSubview:btn];
            btn.frame = CGRectMake(X, Y, kwt(103), kwt(40));
            X += kwt(120);
            if(X > kwt(300)){
                X = kwt(15);
                Y += kwt(52);
            }
        }
        Y += kwt(14);
        if(X < kwt(300)){
            Y += kwt(52);
        }
    }
    self.sxScrollView.contentSize = CGSizeMake(0,Y + kwt(52));
}
- (void)qbBtnclick{///全部按钮点击
    if(self.PositionsArray.firstObject.firstObject.isSeled){
        for (PositionsModel * nameModel in self.PositionsArray.firstObject) {
            nameModel.isSeled = NO;
        }
        self.PositionsArray.firstObject.firstObject.isSeled = NO;
    } else {
        for (PositionsModel * nameModel in self.PositionsArray.firstObject) {
            nameModel.isSeled = NO;
        }
        self.PositionsArray.firstObject.firstObject.isSeled = YES;
    }
    BOOL isSeled = NO;
    for (PositionsModel * model in self.PositionsArray.lastObject) {
        if(model.isSeled && model != self.PositionsArray.lastObject.firstObject){
            isSeled = model.isSeled;
        }
    }
    
    self.SXBtn.selected = isSeled;
    
    [self addSXview];
    [self addSXview];
}
- (void)zwBtnClick:(gywbtn *)btn{
    PositionsModel * model = [btn.infoDict objectForKey:@"model"];
    if(model.isNameType){
        if(model.isSeled == NO){
            for (PositionsModel * nameModel in self.PositionsArray.lastObject) {
                nameModel.isSeled = NO;
            }
            model.isSeled = YES;
        } else {
            model.isSeled = NO;
        }
    } else {
        model.isSeled = !model.isSeled;
        self.PositionsArray.firstObject.firstObject.isSeled = NO;
    }
    BOOL isSeled = NO;
    for (NSArray< PositionsModel * >* modelArray in self.PositionsArray) {
        
        for (PositionsModel * model in modelArray) {
            if(model.isSeled && model != modelArray.firstObject){
                isSeled = model.isSeled;
            }
        }
    }
    self.SXBtn.selected = isSeled;
    
    [self addSXview];
}
-(void)loadNewData{
    
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    if(self.PositionsArray.count > 0){
        NSArray< PositionsModel * >* position = self.PositionsArray.firstObject;
        NSArray< PositionsModel * >* namearray = self.PositionsArray.lastObject;
        NSMutableArray * positionIds = [NSMutableArray array];
        for (PositionsModel * model in position) {
            if(model.isSeled){
                if(model.value.length > 0){
                    [positionIds addObject:model.value];
                }
            }
        }
        [dict setObject:positionIds forKey:@"positionIds"];
        for (PositionsModel * model in namearray) {
            if(model.isSeled){
                [dict setObject:model.value forKey:@"type"];
            }
        }
        
    }
    [SCNetManger PostAppAccountGetAppUserManageWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:PersonnelModel.class json:model.result];
            if(array.count > 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            self.modelArray = [NSMutableArray arrayWithArray:array];
            [self.tableview.mj_header endRefreshing];
            self.countLab.text = [NSString stringWithFormat:@"人员信息(%lu)",self.modelArray.count];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kwt(5);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.modelArray[indexPath.row].cellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    
    PersonnelListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonnelListTableViewCell"];
    if (cell == nil) {
        cell = [[PersonnelListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonnelListTableViewCell"];
    }
    cell.model = self.modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.openRWBlock = ^(PersonnelModel * _Nonnull model) {
        foundQuestViewController * vc = [foundQuestViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        PondUserModel * m = [PondUserModel new];
        m.userId = model.userId;
        m.phone = model.phone;
        m.realName = model.realName;
        m.userIdentity = model.userIdentity;
        vc.usermodel = m;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    return cell ;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonnelDetailsViewController * vc = [PersonnelDetailsViewController new];
    vc.userId =  self.modelArray[indexPath.row].userId;
    [self.navigationController pushViewController:vc animated:YES];
}
;

@end
@implementation PositionsModel
@end
